﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Data;

namespace Ewts.PD
{
    public partial class PDNoAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    DataLib.BLL.City bllLib = new DataLib.BLL.City();
                    
                    //归属地
                    dropBcity.DataSource = bllLib.GetList(" flag1='01'");
                    dropBcity.DataTextField = "cityname";
                    dropBcity.DataValueField = "cityname";
                    dropBcity.DataBind();
                    dropBcity.Items.Insert(0, item);

                    // 处理地
                    dropGcity.DataSource = bllLib.GetList(" flag1='02'");
                    dropGcity.DataTextField = "cityname";
                    dropGcity.DataValueField = "cityname";
                    dropGcity.DataBind();
                    dropGcity.Items.Insert(0, item);

                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    hidUser.Value = curUser.UserName;
                    string city = "";
                    bindData(city);
                }
                else
                {
                    Response.Redirect("/accounts/login.aspx");
                }

            }
           

        }

        public void bindData(string city)
        {
            DataTable dt = new DataTable();
            string strwhere = "(status=7 or status=8) and Left(outStorageNo,1)='P' and Left(OutDepart,Len('" + city + "'))='" + city + "'";
            if (Request["outid"] != null && Request["outid"].ToString().Trim() != "") 
            {
                //strwhere += " and id <> " + Request["outid"].ToString().Trim();
                DataLib.Model.OutStorage model = new DataLib.BLL.OutStorage().GetModel(int.Parse(Request["outid"].ToString().Trim()));
                dropBcity.SelectedValue = model.inDepart;
                dropGcity.SelectedValue = model.OutDepart;
                dropBcity.Enabled = false;
                dropGcity.Enabled = false;
                btnAdd.Text = "备注修改";
                txtmemo.Value = model.memo;
            }
            dt = new DataLib.BLL.OutStorage().GetList(strwhere).Tables[0];
            rept.DataSource = dt;
            rept.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataLib.Model.OutStorage model = new DataLib.Model.OutStorage();
            DataLib.BLL.OutStorage bll = new DataLib.BLL.OutStorage();
            string bcity = dropBcity.SelectedValue;
            string gcity = dropGcity.SelectedValue;
            
            if (bcity == "" || gcity == "")
            {
                DataLib.Common.MessageBox.Show(this.Page, "归属地和处理地不能为空");
                return;
            }

            int flag = 0;
            if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
            {
                model = bll.GetModel(int.Parse(Request["outid"].ToString().Trim()));
                model.memo = txtmemo.Value;
                if (bll.Update(model))
                    flag = 1;   //修改成功
                else
                    flag = -1;  //修改失败
            }
            else
            {
                 if (bll.InsertPDNo(bcity, gcity, hidUser.Value, txtmemo.Value) > 0)
                    flag = 2;
                 else
                     flag = -2;
            }
            switch(flag)
            {
                case 1:
                    DataLib.Common.MessageBox.Show(Page,"修改成功");
                    break;
                case 2:
                    DataLib.Common.MessageBox.Show(Page, "添加成功");
                    break;
                case -1:
                    DataLib.Common.MessageBox.Show(Page, "修改失败");
                    break;
                case -2:
                    DataLib.Common.MessageBox.Show(Page, "添加失败");
                    break;
                default:
                    break;
            }
            Response.Redirect("PDNoAdd.aspx");

        }

        protected string GetUpdateBtn(string outid)
        {
            
            string strhtml ="<span style='color:#ccc;'>维护</span>";
            if (outid != null && outid.Trim() != "0")
            {
                DataLib.Model.OutStorage model = new DataLib.BLL.OutStorage().GetModel(int.Parse(outid));
                if (model != null && model.status == 7)
                {
                    strhtml = "<a href='PDNoAdd.aspx?outid=" + outid + "' target='_self'>维护</a>";
               //btnAdd.Text = "修改";
                }

            }
            return strhtml;
        }
    }
}